package com.lanlian.utillib;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.TextView;

/**
 * 监听输入框字数
 */
public class WorksSizeCheckUtil {

    public interface IEditTextChangeListener {
        void textChange(boolean isHasContent);
    }

    private IEditTextChangeListener mChangeListener;

    public void setChangeListener(IEditTextChangeListener changeListener) {
        this.mChangeListener = changeListener;
    }

    private TextView button;
    private TextView[] editTexts;

    public WorksSizeCheckUtil(TextView button) {
        this.button = button;
    }

    public WorksSizeCheckUtil addAllEditText(TextView... editTexts) {
        this.editTexts = editTexts;
        initEditListener();
        return this;
    }

    private void initEditListener() {
        for (TextView editText : editTexts) {
            editText.addTextChangedListener(new TextChange());
        }
    }

    /**
     * edit输入的变化来改变按钮的是否点击
     */
    private class TextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (checkAllEdit()) {
                Log.i("TAG", "所有edittext有值了");
                mChangeListener.textChange(true);
                button.setEnabled(true);
            } else {
                button.setEnabled(false);
                Log.i("TAG", "有edittext没值了");
                mChangeListener.textChange(false);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    }

    /**
     * 检查所有的edit是否输入了数据
     *
     * @return
     */
    private boolean checkAllEdit() {
        for (TextView editText : editTexts) {
            if (!TextUtils.isEmpty(editText.getText() + "")) {
                continue;
            } else {
                return false;
            }
        }
        return true;
    }

}